/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.common.feedback.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.feedback.entity.Feedback;
import cn.apestar.modules.common.feedback.service.FeedbackService;

/**
 * 反馈建议Controller
 * @author LVH
 * @version 2016-07-12
 */
@Controller
@RequestMapping(value = "${adminPath}/task/feedback")
public class FeedbackController extends BaseController {

    @Autowired
    private FeedbackService feedbackService;

    @ModelAttribute
    public Feedback get(@RequestParam(required = false) String id) {

        Feedback entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = feedbackService.get(id);
        }
        if (entity == null) {
            entity = new Feedback();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(Feedback feedback, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<Feedback> page = feedbackService.findPage(new Page<Feedback>(request, response), feedback);

        for (Feedback fb : page.getList()) {
            System.out.println(fb.getDescription() + "," + fb.getCode());
        }

        model.addAttribute("page", page);
        return "modules/feedback/feedbackList";
    }

    @RequestMapping(value = {"customer"})
    public String listCustomer(Feedback feedback, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<Feedback> page = feedbackService.findCustomerPage(new Page<Feedback>(request, response), feedback);

        for (Feedback fb : page.getList()) {
            System.out.println(fb.getDescription() + "," + fb.getCode());
        }

        model.addAttribute("page", page);
        return "modules/feedback/feedbackList";
    }

    @RequestMapping(value = "form")
    public String form(Feedback feedback, Model model) {

        model.addAttribute("feedback", feedback);
        return "modules/feedback/feedbackForm";
    }

    @RequestMapping(value = "save")
    public String save(Feedback feedback, Model model, RedirectAttributes redirectAttributes) {

        if (!beanValidator(model, feedback)) {
            return form(feedback, model);
        }
        feedbackService.save(feedback);
        addMessage(redirectAttributes, "保存反馈建议成功");
        return "redirect:" + Global.getAdminPath() + "/task/feedback/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(Feedback feedback, RedirectAttributes redirectAttributes) {

        feedbackService.delete(feedback);
        addMessage(redirectAttributes, "删除反馈建议成功");
        return "redirect:" + Global.getAdminPath() + "/task/feedback/?repage";
    }

}